package com.example.software.controller;

import java.util.*;
import java.util.stream.Collectors;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.dto.WorkdayDTO;
import com.example.software.entity.Result;
import com.example.software.vo.workdayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.WorkdayEntity;
import com.example.software.service.WorkdayService;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;



/**
 * 
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-28 10:54:37
 */
@RestController
@Slf4j
@Api(tags = "排班表相关接口")
@RequestMapping("software/workday")
public class WorkdayController {


    @Autowired
    private WorkdayService workdayService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询排班信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department_id", value = "科室id",dataType = "Long"),
            @ApiImplicitParam(name = "doctorId", value = "医生id，如果查全部可以不传或者传0")
    })
   // @RequiresPermissions("software:workday:list")
    public Result<Map<String, List<workdayVO>>> list(
            @RequestParam(required = false) Long department_id,
            @RequestParam(required = false) Long doctorId){

        WorkdayDTO workdayDTO = new WorkdayDTO();
        workdayDTO.setDepartmentId(department_id);
        workdayDTO.setDoctorId(doctorId);

        List<workdayVO> workdayList = workdayService.getWorkdayList(workdayDTO);

        System.out.println(workdayList);


        Map<String, List<workdayVO>> groupedByDepartment2 = new HashMap<>();

        List<workdayVO> list = new ArrayList<>();
        groupedByDepartment2.put("0", list);
        groupedByDepartment2.put("1", list);
        groupedByDepartment2.put("2", list);
        groupedByDepartment2.put("3", list);
        groupedByDepartment2.put("4", list);
        groupedByDepartment2.put("5", list);
        groupedByDepartment2.put("6", list);


        Map<String, List<workdayVO>> groupedByDepartment = workdayList.stream()
                .collect(Collectors.groupingBy(workdayVO::getWorkTime));

        for (String key : groupedByDepartment2.keySet()){
            if (groupedByDepartment.containsKey(key)){
                groupedByDepartment2.put(key, groupedByDepartment.get(key));
            }
        }


        System.out.println(groupedByDepartment2);



//        PageUtils page = workdayService.queryPage(params);

        return Result.success(groupedByDepartment2);

    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据主键id查询排班信息")
    // @RequiresPermissions("software:workday:info")
    public Result<workdayVO> info(@PathVariable("id") Long id){



		WorkdayEntity workday = workdayService.getById(id);

        workdayVO workdayVO = new workdayVO();
        BeanUtils.copyProperties(workday, workdayVO);

        return Result.success(workdayVO);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增排班信息")
    // @RequiresPermissions("software:workday:save")
    public R save(@RequestBody WorkdayDTO workdayDTO){

        WorkdayEntity workday = new WorkdayEntity();

        //属性拷贝
        BeanUtils.copyProperties(workdayDTO, workday);

		workdayService.save(workday);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改排班信息")
    // @RequiresPermissions("software:workday:update")
    public R update(@RequestBody WorkdayDTO workdayDTO){

        WorkdayEntity workday = new WorkdayEntity();

        //属性拷贝
        BeanUtils.copyProperties(workdayDTO, workday);

		workdayService.updateById(workday);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除排班信息")
    // @RequiresPermissions("software:workday:delete")
    public R delete(@RequestBody Long[] ids){
		workdayService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
